conqueraifandomcom-20200214-history
Scripting Section
Introduction to Scripting ConquerAI use Iron.Python as it's scripting language. Python is taught at many universities as a computer science course, and even in some high schools. It is a simple scripting-based object-oriented language. ConquerAI scripts use a Linear scripting logic which means it doesn't require as much object-oriented knowledge, nor considering non-linear programming. So this means it's much more easier to learn then plugins (C#). What you will need to start Scripting You will first need a basic knowledge of the Python language, there are many guides on the internet on learning this language including the Python site or you can learn the language as you code by studying other peoples sources. You will also need a text editor, using the notepad program that comes with your OS is fine but NotePad++ is a much better option. To View all the API you will need to have installed Microsoft Visual C# 2010 Express, this is a free program that is needed to build plugins but also view the API. Having the site Pastebin handy is also a good idea, many times you will want a fellow botter or Mod/Admin to take a look at you code, Pastebin is a much cleaner and easier way to share it then Forum private messages. Viewing the scripts API Being able to view the scripts syntax is a must, there is no point knowing the language but being unable to do anything with it. To view ConquerAI's API you will need to have Microsoft Visual C# 2010 Express (MVCE) and ConquerAI installed. 'Steps Involved' 1: Open up MVCE. 2: Once open Click on "New Project". 3: Choose the "Class Library" template. 4: Now you should have on the right side a "Solution Explorer" section. 5: Right click on "References" and Click "Add Reference". 6: A new dialogue box should have popped up, click the "Browse" tab. 7: Find the "COFarmerPluginLibrary.dll" located in the ConquerAI directory. 8: Once you have clicked "Ok" the COFarmer Plugin Library will be added. 9: On the Right side of the screen double click on "COFarmerPluginLibrary". 10: Now in the "Object Browser" press the "+" next to COFarmerPluginLibrary. 11: Another "+" will appear below it, click that one too. 12: Now you have all ConquerAI's API to view. 13: Clicking on the components name will show the syntax, eg: "Action Mode" Steps 3, 5, 8 and 13 are picture of what should be done. Note: When first opening MVCE there may not be a "Solution Explorer" on the right side of the screen, to bring it up click the Magnifying/folder/window icon up the top. Saving and editing scripts There are two ways you can save a ConquerAI script, you can save it as a .py file (ScriptName.py) or as en ecrypted .pyx file (ScriptName.pyx). When saving your script in .py format you are allowing anyone to view and edit your code, in .pyx format no one can edit or view your code. All scripts should be saved in the "script" folder in the Conquer AI directory "ConqerAI_Directory\Plugins\Resource\Script" They can not be run or encrypted out of this folder. Saving your script in .py format is as simple as it sounds, just save the text document as ScriptName.py or in Notepad++ select the Python option. Editing .py script is as simple as double clicking on it. If you want to encrypt your script you will have to login the bot and Conquer Online. Once you have a character open in Conquer Online (Run via the bot) type in the chat section @pyx ScriptName.py key . Example: @pyx MoonBox.py 123456 Now a .pyx file has been created in you scripts folder and your original .py file is still there to be edited. Note: Every time you want to update of your script in .pyx format, you will have to do the process again. Running a script Scripts are quite easy to run. There are two ways to run a script, either in game via the chat area or in the bot's GUI. Running the script in game is mainly used for testing scripts or for monitoring what the character is doing whilst the script is running. Ideally Scripts should run Clientlessly. The command to run a script in game is @pyscript ScriptName.py/pyx . Example: @pyscript MoonBox.py or @pyscript MoobBox.pyx Running a script Via the GUI is mainly used for a character already logged in Clientlessly. This can be done by selecting the the character on the left side of the screen and click the "General Tab". At the bottom of this tab there is a field that you can place the command in to run the script. Example: pyscript MoonBox.py or pyscript MoobBox.pyx Notice how the "@" (Prefix) is not needed. 'Running a script that has an argument' Some scripts have arguments that you can choose from, and example of this kind of script is Chalkie's SMT script. An example of how to run an argument script will be below. Example: @pyscript SMT.pyx AUS-NSW Or for the GUI pyscript SMT.pyx AUS-NSW This example is using Chalkie's SMT script. "@pyscript/''pyscript''" is the command, "SMT.pyx" is the script file and "AUS-NSW" Is the argument. Scripting Errors When running a script you may receive an error message in ConquerAI's Log/Debug window. There are few different types of errors that can occur. 'Syntax/Indent error' Indent errors are caused when you have not spaced out your code properly. This error will be shown in the Log/Debug window. Example: 09:10:09:3125-CharacterNamePython Script Error: Microsoft.Scripting.SyntaxErrorException: expected an indented block Fixing this is as simple as re-reading your code and fixing the spacing errors. Example: Error: def start(): if (Client.Level > 1): Fixed: def start(): if (Client.Level > 1): This was fixed by adding the correct indent (spacing) the the code. 'Unbound Name Exception error' This error is caused by and invalided syntax, the code you are trying to use may not exist or could be a simple typo. Example: 09:16:03:4218-CharacterNamePython Script Error: IronPython.Runtime.UnboundNameException: global name 'Vlient' is not defined When this error appears it will tell you the problem name, you can then search for it and fix it. Example: The Error is 09:16:03:4218-CharacterNamePython Script Error: IronPython.Runtime.UnboundNameException: global name 'Vlient' is not defined Look for the section if (Vlient.Level > 1): Fix the problem if (Client.Level > 1): 'Scripting Argument Type Exception' This error is caused when you have made to many or not enough arguments. When selecting the API in MVCE it will tell you how many and what arguments you must use. Example: 09:24:56:2500-CharacterNamePython Script Error: Microsoft.Scripting.ArgumentTypeException: CastSpell() takes at least 2 arguments (1 given) The error here I have only given one argument when two is needed. To fix this you will need to re-look at the API to see what you have missed. Example: Error Code is Client.CastSpell(1095) A Fix would be Client.CastSpell(Client.UID, 1095) Now that two arguments are given this would no longer throw a SARE error. 'Custom errors' Some script writers will create there own custom errors. How these display is up to the script maker them self's, some will show it in game some will show it via the Log/Debug section or both. The error that is displayed should tell you the reason why but it is up the script maker to provide a reason. Example: Chalkie's Meteor Tear Quest. 09:35:00:4687-CharacterNameERROR You started the script with no meteors in your inventory, script stopped. This error clearly states you that you have no Meteors so it will not continue. My First Script In this section we will be creating our first script to run in ConquerAI. First off open your desired editor (Notepad, NotePad++ or the bots built in scripting window.) Before we start coding we will save the file as a .py file, so in your editor go to File, Save as and type MyFirstScript.py (In Notepad++ there will be a option on the drop down menu for Python Script) and click Save. 'Hello, Conquer World.' Now we have our script page open paste or type the following in. import clr clr.AddReference("COFarmerPluginLibrary") from COFarmerPluginLibrary import * from System import * This is basically telling the bot to use the Plugin Library as a refrence, This is needed at the top of every script. We will now add a line of code for the script to run, press enter and paste/type the following in. Client.Chat("Hello, Conquer World", ChatMode.GM, PacketSource.ToClient) This line of code will display a message on your conquer window once run. Client.Chat - This is the command. "Hello, Conquer World" - This is what is to be said/displayed. ChatMode.GM - This is the message type, in this case it's GM system message. PacketSource.ToClient - This means it is only going from the bot to your Client so no one else will see it. Now save the code and go to your Conquer window and type in the chat @pyscript MyFirstScript.py and press enter. You should see a system message saying "Hello, Conquer World". 'Getting a display message in the bots log/debug screen.' Knowing how to make a message display in the log/debug is important too. This will allow you to know whats going on with a script if it is being run Clientlessly. This method is no more complex then adding a message in game, simply press enter for a new line and type/paste. Client.Log("Hello down here as well") Now this is added the script should say both. import clr clr.AddReference("COFarmerPluginLibrary") from COFarmerPluginLibrary import * from System import * Client.Chat("Hello, Conquer World", ChatMode.GM, PacketSource.ToClient) Client.Log("Hello down here as well") Your script should look like this. 'How do I get other people to see my messages?' Some time you will want other people in the server to see the messages you are sending, spam script is an example. This is done by changing the ToClient to ToServer or FromClient. Not all messages can be sent to the server, an example of this is the GM system message. It can only be seen as a ToClient packet source. Client.Chat("Hello, Conquer World", ChatMode.Talk, PacketSource.ToClient) This would display as a Talk message from your character. 'How to target other people with a message?' To target other people with yout message just add there name to the end of the line of the command. People with random ASCII characters will throw an error, an example of that is the ™ character. Client.Chat("Hello Chalkie", ChatMode.Talk, PacketSource.ToClient, "Chalkie") This would display a talk message to Chalkie "(Talk) John speaks to Chalkie: Hello Chalkie" 'Moving in a script.' Moving somewhere in a script is very simple and thanks to the coders very fast. Before typing in the line of command find somewhere you want to move to and type down the location. The example I will be using is moving to the TwinCitys farm. BCOHelper.LinearTravel(Client, 1002, 347, 102) This line of code will move your character to that coordinates. Linear travel was created in a way that the next part of the script will not move on until the character is at the coordinates you have set, this mean no sleep times are needed between travel, it also means that the bot will take into account all the portals needed to get there resulting in faster movement and less sleep time. This movement how ever can have it's draw backs on users with slow internet or a laggy server. Because Linear Travel uses Portal to Portal travel to get to it's destination with lag it can result in DC or too many portals. This Is an example: No lag. TwinCity to the second Saddle cave. Portal 4 - Portal 3 - Portal 1 -Portal 1 Saddle cave. With lag to the second Saddle cave. Portal 4 - Portal 3 - Portal 1 -Portal 1...No response trying Portal 1 again, success moved. The problem with this is now it has moved 3 portal 1's by the time the lag has caught up, resulting in you being in Saddle cave 3. This can be "fixed" by adding fail safes to your script (will be explained later). Most people should not have this portal problem, it can occur with a laggy server/connection or with a lot of miners loged in. This should not be a problem if your script is not moving map to map. If statements and sections. In more advanced scripts you will need to create sections for the script to run to. Sections will allow you to have more then one variable for the script to run with out having to use multiple scripts. A basic section example would be. def Section1(): Client.Log("Section1") Section2() def Section2(): Client.Log("Section2") This would run through the first section, do all the instructions then move onto the next section. This can be used in a much more advanced for show in Chalkies SMT script where arguments allow you to start a diffrent section of the script. if (args0 "Section1") : Section1() if (args0 "Section2") : Section2() def Section1(): Client.Log("Section1") def Section2(): Client.Log("Section2") This would allow you to run the section 2 with out running the first section. 'If statements' If statements allows the script to move to different sections depending on the variables you have instructed it to check. You can have more then on if statement in the one section as well as the else statement. An example of this would be checking the characters level. def Chklevel(): if(Client.Level 10) : StartLevling() This would check the characters level and if it was level 10 it would go to the next section. 'Adding else statment'. Adding else statements is a must if you need the script to do something else if the character is not level 10. def ChkLevel(): if(Client.Level 10) : StartLeveling() else: Client.Log("Character is not level 10") Now that a else statement has been added the script will still do something if the character is not level 10. 'Adding elif statement ' This is use full if you want the script to check multiple veriables. def ChkLevel(): if(Client.Level 10) : StartLeveling10() elif(Client.Level 7) : StartLeveling7() elif(Client.Level 5) : StartLeveling5() else: Client.Log("Character is not level 10, 7 or 5") This will give the script more options to move to different sections if the first variable is not met. Remember means equal to, you can make it => Equal to or more then, =< Equal to or less then, < Less then, > More then..